<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CoDistribution;
use App\Models\CoSchool;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class CoDistributionController extends AdminController
{
    protected $title = '配送员管理';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CoDistribution(), function (Grid $grid) {
            $grid->model()
                ->where("is_shen",1)
                ->where('is_type',1)
                ->where('deleted_at','=',null)
                ->paginate(10);
            $grid->id->sortable();
            $grid->column('truename','姓名');
            $grid->avatar->image('',40,40);
            $grid->column('tel','电话');
            $grid->gender->display(function ($val) {
                if ($val == 1) {
                    return '男';
                } else {
                    return "女";
                }
            });
            $grid->idcard;
            $grid->student_number;
            $grid->balance;
            $grid->regist_date;
            $grid->column('status','状态')->switch();
            $grid->updated_at->sortable();
            $grid->disableCreateButton();
//            $grid->disableFilter();
            $grid->actions(function ($actions) {

                // 去掉删除
                $actions->disableDelete();

                // 去掉编辑
                $actions->disableEdit();

                // 去掉查看
//                $actions->disableView();
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('school_id','学校')
                    ->select(CoSchool::where('status',1)->pluck('name','id'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoDistribution(), function (Show $show) {
            $show->id;
            $show->truename;
            $show->avatar->image();
            $show->tel;
            $show->gender->as(function ($v){
                if($v ==1){
                    return '男';
                }else{
                    return '女';
                }
            });
            $show->idcard;
            $show->student_number;
            $show->idcard_img_r->image();
            $show->idcard_img_l->image();
            $show->studentcard_img_r->image();
            $show->studentcard_img_r->image();
            $show->balance;
            $show->regist_date;
            $show->created_at;
            $show->updated_at;
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoDistribution(), function (Form $form) {
            $form->display('id');
            $form->display('truename','名称');
            $form->display('avatar','头像')->with(function ($value) {
                return "<img src='$value' style='width: 150px;height: 150px;' />";
            });
            $form->text('status');
            $form->text('tel','电话');
            $form->display('gender','性别')->with(function ($value) {
                if($value == 1){
                    return '男';
                }else{
                    return '女';
                }
            });
            $form->display('idcard');
            $form->display('student_number');
            $form->display('idcard_img_r')->with(function ($value) {
                return "<img src='$value' style='width: 150px;height: 150px;' />";
            });
            $form->display('idcard_img_l')->with(function ($value) {
                return "<img src='$value' style='width: 150px;height: 150px;' />";
            });
            $form->display('studentcard_img_r')->with(function ($value) {
                return "<img src='$value' style='width: 150px;height: 150px;' />";
            });
            $form->display('studentcard_img_r')->with(function ($value) {
                return "<img src='$value' style='width: 150px;height: 150px;' />";
            });
            $form->display('balance');
            $form->display('regist_date');
            $form->display('created_at');
            $form->display('updated_at');
            $form->disableDeleteButton();
        });
    }
}
